type DivisionType = ["时", "分", "秒"] | ["h", "m", "s"] | ":" | "-";

export const dateFormat = {
	getDateTime(date: any) {
		if (!Number.isNaN(Number(date))) {
			date = Number(date);
		}
		if (!date) {
			return "";
		}
		let stamp = 0;
		try {
			if (isNaN(Number(date))) {
				stamp = new Date(date).getTime();
			} else {
				stamp = new Date(Number(date)).getTime();
			}
		} catch (e) {
			console.error(e);
			return "";
		}
		return stamp;
	},

	/**
	 * 日期时间过滤
	 * @param date 日期对象，字符串，时间戳
	 * @param type 分隔符
	 * @param format 默认：年月日, 1：年月日时分秒 2：时分秒
	 * @returns
	 */
	formatTime(date: string | number | Date, type: string = "/", format: number = 0) {
		if (!Number.isNaN(Number(date))) {
			date = Number(date);
		}
		if (!date) {
			return "";
		}
		let stamp = 0;
		try {
			stamp = new Date(date).getTime();
		} catch (e) {
			console.error(e);
			return "";
		}
		const now = new Date(stamp);
		const year = now.getFullYear();
		let month: string | number = now.getMonth();
		let curDate: string | number = now.getDate();

		// let day = now.getDay();// 得到周几
		let hour: string | number = now.getHours();
		let minu: string | number = now.getMinutes();
		let sec: string | number = now.getSeconds();
		month = month + 1;
		if (month < 10) {
			month = "0" + month;
		}
		if (curDate < 10) {
			curDate = "0" + curDate;
		}
		if (hour < 10) {
			hour = "0" + hour;
		}
		if (minu < 10) {
			minu = "0" + minu;
		}
		if (sec < 10) {
			sec = "0" + sec;
		}
		let time = "";
		// 精确到天
		if (format === 1) {
			// 年月日时分秒
			time = year + type + month + type + curDate + " " + hour + ":" + minu + ":" + sec;
		} else if (format == 2) {
			// 时分秒
			time = hour + ":" + minu + ":" + sec;
		} else {
			// 年月日
			time = year + type + month + type + curDate;
		}
		return time;
	},
	/**
	 * 时间戳转换
	 * @param data 时间戳
	 * @param division 格式展示
	 * @returns
	 */
	formatDuration(data: number, division: DivisionType = ["时", "分", "秒"]) {
		if (!data || data == 0) return [0, 0, 0];
		let hr: number | string = parseInt(`${data / (1000 * 60 * 60)}`);
		let min: number | string = parseInt(`${(data % (1000 * 60 * 60)) / (1000 * 60)}`);
		let sec: number | string = parseInt(`${(data % (1000 * 60)) / 1000}`);

		function splitDuration() {
			if (Array.isArray(division)) {
				if (Number(hr) <= 0) {
					if (Number(min) <= 0) {
						return `${sec}${division[2]}`;
					}
					return `${min}${division[1]}${sec}${division[2]}`;
				}
				return `${hr}${division[0]}${min}${division[1]}${sec}${division[2]}`;
			} else {
				return [hr, min, sec];
			}
		}

		return splitDuration();
	},
	/**
	 * 0：未传入时间 1：小于开始时间 2：开始时间与结束时间中间 3：大于结束时间
	 * @param start 开始时间
	 * @param end 结束时间
	 * @returns
	 */
	compare(start: string | number | Date, end: string | number | Date) {
		if (!start) return 0;
		if (!end) return 0;
		const curTime = new Date().getTime();
		if (curTime < new Date(start).getTime()) return 1;
		if (curTime > new Date(start).getTime() && curTime < new Date(end).getTime()) return 2;
		if (curTime > new Date(end).getTime()) return 3;
	},
};
export const dateDiff = (hisTime: string) => {
	const now = new Date().getTime();
	const diffValue = now - new Date(hisTime).getTime();
	const minute = 1000 * 60;
	const hour = minute * 60;
	const day = hour * 24;
	const month = day * 30;
	const year = month * 12;

	const _year = diffValue / year;
	const _month = diffValue / month;
	const _week = diffValue / (7 * day);
	const _day = diffValue / day;
	const _hour = diffValue / hour;
	const _min = diffValue / minute;
	if (_year >= 1) return parseInt(_year.toString()) + "年前";
	else if (_month >= 1) return parseInt(_month.toString()) + "个月前";
	else if (_week >= 1) return parseInt(_week.toString()) + "周前";
	else if (_day >= 1) return parseInt(_day.toString()) + "天前";
	else if (_hour >= 1) return parseInt(_hour.toString()) + "个小时前";
	else if (_min >= 1) return parseInt(_min.toString()) + "分钟前";
	else return "刚刚";
};
/**
 * 获取未来多少分钟
 * @param timeStr
 * @param duration  时长，默认15分钟
 * @returns
 */
export const getFuture = (timeStr: string, duration: number = 1000 * 60 * 15) => {
	const now = new Date(timeStr);
	const time = now.getTime() + duration;
	return dateFormat.formatTime(new Date(time).getTime(), undefined, 2);
};
